package com.lg.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lg.mapper.JobExecutionLogMapper;
import com.lg.mapper.UserMapper;
import com.lg.mapper.UserRoleMapper;
import com.lg.pojo.JobExecutionLog;
import com.lg.pojo.JobExecutionLogExample;
import com.lg.pojo.Role;
import com.lg.pojo.User;
import com.lg.pojo.UserRole;
import com.lg.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private JobExecutionLogMapper mapper;
	
	//@Transactional
	public void login() {
		PageHelper.startPage(1, 5);
		
		JobExecutionLogExample example =  new JobExecutionLogExample();
		
		List<JobExecutionLog> selectByExample = mapper.selectByExample(null );
		PageInfo<JobExecutionLog> pageInfo;
		if(selectByExample instanceof Page) {
			 pageInfo = new PageInfo<>(selectByExample);
		}
	}
	
	@Override
	@Transactional
	public void addUser(User user) {
		Md5Hash md5Hash = new Md5Hash(user.getPassword(), user.getSalt(), 1);
		user.setPassword(md5Hash.toString());
		user.setState((byte) 0);
		userMapper.insert(user);
		List<Role> roles = user.getRoles();
		if (roles != null) {
			roles.forEach(r -> {
				Integer id = r.getId();
				Integer uid = user.getUid();
				UserRole record = new UserRole();
				record.setUid(uid);
				record.setRoleId(id);
				userRoleMapper.insert(record);
			});
		}
	}

	@Override
	public User findFullUserById(Integer i) {
		// TODO Auto-generated method stub
		return userMapper.selectFulluserByPrimaryKey(i);
	};
	@Override
	public User findFullUserById2(Integer i) {
		// TODO Auto-generated method stub
		return userMapper.selectFulluserByPrimaryKey2(i);
	};
}
